package com.example.lowbird.lowbird.utils;

import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.view.View;

import com.activeandroid.Model;
import com.activeandroid.query.Delete;
import com.activeandroid.query.Select;
import com.activeandroid.query.Update;
import com.activeandroid.util.Log;
import com.example.lowbird.lowbird.R;
import com.example.lowbird.lowbird.app.Myapp;
import com.example.lowbird.lowbird.fragment.ShopingFragment;
import com.example.lowbird.lowbird.java.SHopcar;


import java.util.List;

import static android.support.v4.content.ContextCompat.startActivity;


/**
 * Created by Administrator on 2016/11/3.
 */

public class Dbutils {

    public static List<SHopcar> getShopcar() {
        Select select = new Select();
        List<SHopcar> list = select.from(SHopcar.class).execute();
        android.util.Log.e("SHOPDb", "getShopcar: " + list.size());
        return list;
    }

    public static void deletShop(String name) {
        Delete delete = new Delete();
        //删除所有数据
//        delete.from(SHopcar.class).execute();

        delete.from(SHopcar.class).where("name like ?", name).execute();


    }


    public static void updata(String name, int count) {

        Update update = new Update(SHopcar.class);
        update.set("count = ?", count).where("name = ? ", name).execute();

        ShopingFragment.initlistdata();


    }

    public static void updataCheck(String name, int flag) {

        Update update = new Update(SHopcar.class);

        update.set("selected = ?", flag).where("name = ? ", name).execute();
        ShopingFragment.showprice();


    }

    public static void createShop(int id2, String imageurl, String name, int count, double price, int maxsize, String shopurl) {


        boolean flag = true;
        List<SHopcar> shopcar1 = getShopcar();

        for (int i = 0; i < shopcar1.size(); i++) {

            if (shopcar1.get
                    (i).getName().equals(name)) {
                flag = false;

                break;

            }

        }
        if (flag) {
            SHopcar shopcar = new SHopcar(id2,imageurl, name, count, price, maxsize, shopurl, 1);

            android.util.Log.e("SHOPCAR", "createShop222:建立数据库成功 ");


            shopcar.save();

            ShopingFragment.initlistdata();

        } else {

            android.util.Log.e("SHOPCAR", "createShop:已经存在了哦 ");

        }

    }

    public static void createShop(SHopcar shopcar) {


        boolean flag = true;
        List<SHopcar> shopcar1 = getShopcar();

        for (int i = 0; i < shopcar1.size(); i++) {

            if (shopcar1.get
                    (i).getName().equals(shopcar.getName())) {
                flag = false;

                break;

            }

        }
        if (flag) {


            shopcar.save();

            ShopingFragment.initlistdata();

        } else {

            android.util.Log.e("SHOPCAR", "createShop:已经存在了哦 ");

        }

    }

    public static void deletoneShop(String name) {
        Delete delete = new Delete();
        //删除所有数据
//        delete.from(SHopcar.class).execute();

        delete.from(SHopcar.class).where("name like ?", name).execute();

        ShopingFragment.initlistdata();

    }
}
